/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package t7;

/**
 *
 * @author trungdung
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.DataOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;


public class HttpMonster {
    private static String MonsterGet(String url) {
        String r="";
      HttpURLConnection connection = null;
      OutputStreamWriter wr = null;
      BufferedReader rd  = null;
      StringBuilder sb = null;
      String line = null;
    
      URL serverAddress = null;
    
      try {
          serverAddress = new URL(url);
          //set up out communications stuff
          connection = null;
        
          //Set up the initial connection
          connection = (HttpURLConnection)serverAddress.openConnection();
          connection.setRequestMethod("GET");
          connection.setDoOutput(true);
          connection.setReadTimeout(10000);
                    
          connection.connect();
        
          //get the output stream writer and write the output to the server
          //wr = new OutputStreamWriter(connection.getOutputStream());
          //wr.write("");
          //wr.flush();
        
          //read the result from the server
          rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          sb = new StringBuilder();
        
          while ((line = rd.readLine()) != null)
          {
                sb.append(line + '\n');
          }
        
          //System.out.println(sb.toString());
          String s= sb.toString();
          r=s.substring(0,s.length()-1);
                    
      } catch (MalformedURLException e) {
          e.printStackTrace();
          r="401"; // sai url
      } catch (ProtocolException e) {
          e.printStackTrace();
          r="402"; // truyen loi
      } catch (IOException e) {
          e.printStackTrace();
          r="403"; // loi io
      }
      finally
      {
          //close the connection, set all objects to null
          connection.disconnect();
          rd = null;
          sb = null;
          wr = null;
          connection = null;
      }        
        return r;
    }
    private static int MonsterPost(String url,String param) {
        URL Url;
        HttpURLConnection connection = null;  
        try {
          //Create connection
          Url = new URL(url);
          connection = (HttpURLConnection)Url.openConnection();
          connection.setRequestMethod("POST");
          connection.setRequestProperty("Content-Type", 
               "application/x-www-form-urlencoded");

          connection.setRequestProperty("Content-Length", "" + 
                   Integer.toString(param.getBytes().length));
          connection.setRequestProperty("Content-Language", "en-US");  

          connection.setUseCaches (false);
          connection.setDoInput(true);
          connection.setDoOutput(true);

          //Send request
          DataOutputStream wr = new DataOutputStream (
                      connection.getOutputStream ());
          wr.writeBytes (param);
          wr.flush ();
          wr.close ();

          //Get Response	
          BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          String line=rd.readLine();
          //StringBuffer response = new StringBuffer(); 
          //while((line = rd.readLine()) != null) {
          //  response.append(line);
          //  response.append('\r');
          //}
          rd.close();
         // return response.toString();
          //System.out.println(line);     
          try {
              return Integer.parseInt(line);            
          }catch(NumberFormatException e) {
              //System.out.println(line); 
              return 404;           
          }

      } catch (MalformedURLException e) {
          e.printStackTrace();
          return 405; // sai url
      } catch (ProtocolException e) {
          e.printStackTrace();
          return 406; // truyen loi
      } catch (IOException e) {
          e.printStackTrace();
          return 407; // loi io
      }
          finally {

          if(connection != null) {
            connection.disconnect(); 
          }
        }        

    }
    
    private static String ip = "http://123.29.67.203/xoz/test/";
    
    // lay ve ton dau
    public static String laytondau() {
        //System.out.println(ip+"laytondau.php?ca="+ca);
        return MonsterGet(ip+"laytondau.php");
        //return "test";
    }
    
    // nhap du lieu ca
    public static int nhapdulieu(int ca,int nvid,String data) {
        String d="";
        try {
            //System.out.printf(data);
            d="data="+URLEncoder.encode(data,"UTF-8")+"&ca="+ca+"&nvid="+nvid;
            return MonsterPost(ip+"nhap.php",d);
            
        } catch(UnsupportedEncodingException e) {
            e.printStackTrace();
            return 204; //Loi PHP or MYSQL
        }

    }
    
    // yeu cau login HT
    public static int login(int userid,String pass) {
        String param="";
        try {
            param="userid="+URLEncoder.encode(Integer.toString(userid),"UTF-8")+"&password="+URLEncoder.encode(pass,"UTF-8");
            //System.out.println(param);
            //System.out.println("test ne :" +MonsterPost(ip+"login.php",param));
            return MonsterPost(ip+"login.php",param);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return 101;
        }
    }
    
    // lay ttin nvien sau khi login thanh cong
    public static String getnvinfo(int userid) {
        return "";
    }
}




